Imports DotNetPM.Business.Questionnaire

Partial Class Modules_Questionnaires_InstrumentDetail
    Inherits baseWebPage


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetState()
        If Not IsPostBack Then
            RetrieveStateSession()
        End If
        If Not String.IsNullOrEmpty(Request.QueryString("questionId")) Then
            'AddVisible(True)
            lstQuestions.SelectedValue = Request.QueryString("questionId")
        End If
        'Response.Write("Page_Load<br/>")
    End Sub

    Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
        lbutNew.Visible = IsNotLocked()
        'Response.Write("Page_PreRenderComplete : " & (ViewState("IsNotLocked")) & "<br/>")
    End Sub

    Protected Function IsNotLocked() As Boolean
        Return CType(ViewState("IsNotLocked"), Boolean)
    End Function

    Protected Sub gvQuestions_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvQuestions.PageIndexChanged
        AddVisible(False)
        EditVisible(False)
        gvQuestions.SelectedIndex = -1
        lstQuestions.SelectedIndex = -1
        ibutBack.Focus()
    End Sub

    Protected Sub gvQuestions_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvQuestions.RowCommand
        'Response.Write(e.CommandArgument)
        Select Case e.CommandName
            Case "EditLogic"
                ' Convert the row index stored in the CommandArgument
                ' property to an Integer.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument) _
                    - gvQuestions.PageIndex * gvQuestions.PageSize
                ' Retrieve the row that contains the button clicked 
                ' by the user from the Rows collection.
                Dim row As GridViewRow = gvQuestions.Rows(index)
                Session("questionnaire_questionId") = CType(row.FindControl("hfQuestionID"), HiddenField).Value
                Session("questionnaire_currentGuid") = CType(row.FindControl("hfGUID"), HiddenField).Value
                StoreStateSession()
                Session("questionnaire_logicLevel") = 1
                Response.Redirect("InstrumentLogic.aspx")
            Case "SelectQuestion"
                Dim index As Integer = Convert.ToInt32(e.CommandArgument) _
                    - gvQuestions.PageIndex * gvQuestions.PageSize
                gvQuestions.SelectedIndex = index
                Dim row As GridViewRow = gvQuestions.SelectedRow
                EditVisible(True)
                lstQuestions.Enabled = CBool(CType(row.FindControl("hfEnable"), HiddenField).Value)
                ddlCategories.Enabled = lstQuestions.Enabled
                lbutCreate.Enabled = lstQuestions.Enabled
                txtStringToSearch.Enabled = lstQuestions.Enabled
                butSearch.Enabled = lstQuestions.Enabled
                butClear.Enabled = lstQuestions.Enabled
                lstQuestions.SelectedValue = CType(row.FindControl("hfQuestionID"), HiddenField).Value
            Case Else
                'Response.Write("CommandeName:" & e.CommandName)
        End Select
    End Sub


    Protected Sub dvQuestionnaire_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles dvInstrument.ItemUpdating
        Session("questionnaire_instrumentId") = e.NewValues.Item("InstrumentID").ToString.Trim
    End Sub

    Private Sub EditVisible(ByVal visibility As Boolean)
        ibutTop.Visible = visibility
        ibutUp.Visible = visibility
        ibutDown.Visible = visibility
        ibutButtom.Visible = visibility

        trAdd.Visible = visibility
        lbutNew.Visible = Not visibility
        ibutBack.Visible = Not visibility
        butAdd.Visible = Not visibility
        butUpdate.Visible = visibility
    End Sub

    Private Sub AddVisible(ByVal visibility As Boolean)
        trAdd.Visible = visibility
        lbutNew.Enabled = Not visibility
        ibutBack.Visible = Not visibility
        'gvQuestions.Columns(3).Visible = Not visibility
        butAdd.Visible = visibility
        butUpdate.Visible = Not visibility
    End Sub

    Protected Sub butAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butAdd.Click
        Dim instrumentId As String = Session("questionnaire_instrumentId")
        If String.IsNullOrEmpty(instrumentId) Then
            Response.Redirect("InstrumentManager.aspx")
        End If
        InstrumentODS.AddInstrumentQuestion(instrumentId, lstQuestions.SelectedValue)
        gvQuestions.DataBind()
        gvQuestions.PageIndex = gvQuestions.PageCount - 1
        lstQuestions.SelectedIndex = -1
        AddVisible(False)
    End Sub

    Protected Sub butCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butCancel.Click
        AddVisible(False)
        EditVisible(False)
        gvQuestions.SelectedIndex = -1
        lstQuestions.SelectedIndex = -1
    End Sub

    Protected Sub butPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butPreview.Click
        SetPreviousPage(GetState())
        Response.Redirect("QuestionPreview.aspx?qid=" & lstQuestions.SelectedValue)
    End Sub


    Protected Sub ibutBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibutBack.Click
        Response.Redirect("InstrumentManager.aspx")
    End Sub

    Protected Sub lbutNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbutNew.Click
        AddVisible(True)
        lstQuestions.Enabled = True
        ddlCategories.Enabled = lstQuestions.Enabled
        lbutCreate.Enabled = lstQuestions.Enabled
        txtStringToSearch.Enabled = lstQuestions.Enabled
        butSearch.Enabled = lstQuestions.Enabled
        butClear.Enabled = lstQuestions.Enabled
    End Sub

    Protected Sub lbutCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbutCreate.Click
        MyBase.SetPreviousPage(GetState)
        Response.Redirect("QuestionCreate.aspx")
    End Sub


#Region "Editing"

    Protected Sub butUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butUpdate.Click
        Dim row As GridViewRow = gvQuestions.Rows(gvQuestions.SelectedIndex)
        'Response.Write("hfGUID = " & CType(row.FindControl("hfGUID"), HiddenField).Value & "<br/>")
        InstrumentODS.UpdateInstrumentQuestion( _
            CType(row.FindControl("hfGUID"), HiddenField).Value, _
            lstQuestions.SelectedValue)
        gvQuestions.DataBind()
        gvQuestions.SelectedIndex = -1
        EditVisible(False)
    End Sub

    Protected Sub ibutTop_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibutTop.Click

    End Sub

    Protected Sub ibutUp_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibutUp.Click

    End Sub

    Protected Sub ibutDown_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibutDown.Click

    End Sub

    Protected Sub ibutButtom_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibutButtom.Click

    End Sub
#End Region

#Region "Search Question"

    Protected Sub butSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSearch.Click
        Session("questionnaire_category") = ddlCategories.SelectedValue
    End Sub

    Protected Sub butClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butClear.Click
        ddlCategories.SelectedIndex = 0
        Session("questionnaire_category") = 0
        txtStringToSearch.Text = String.Empty
    End Sub

    Protected Sub ddlCategories_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCategories.DataBound
        'Response.Write("ddlCategories_DataBound<br/>")
        ddlCategories.SelectedValue = Session("questionnaire_category")
    End Sub

#End Region


    Private Function GetState(Optional ByVal IsSession As Boolean = False) As String
        Dim state As String = "prevstate="
        state &= gvQuestions.SelectedIndex
        state &= "$"
        state &= gvQuestions.PageIndex
        state &= "$"
        state &= lstQuestions.SelectedValue
        state &= "$"
        state &= lstQuestions.Enabled
        state &= "#editarea"
        Return state
    End Function

    Private Sub SetState()
        Dim queryString As String = Request.QueryString("prevstate")
        If String.IsNullOrEmpty(queryString) Then
            Exit Sub
        End If
        'Response.Write(Server.HtmlDecode(queryString))
        Dim states As String() = queryString.Split("$")
        gvQuestions.SelectedIndex = CInt(states(0))
        gvQuestions.PageIndex = CInt(states(1))
        If Not String.IsNullOrEmpty(states(2)) Then
            lstQuestions.SelectedValue = states(2)
        End If
        If states(0) < 0 Then
            AddVisible(True)
            lstQuestions.Enabled = True
        Else
            EditVisible(True)
            lstQuestions.Enabled = states(3).Split("#")(0)
        End If
        ddlCategories.Enabled = lstQuestions.Enabled
        lbutCreate.Enabled = lstQuestions.Enabled
        txtStringToSearch.Enabled = lstQuestions.Enabled
        butSearch.Enabled = lstQuestions.Enabled
        butClear.Enabled = lstQuestions.Enabled
        Session("questionnaire_instrumentdetail_aspx") = String.Empty
    End Sub

    Private Sub StoreStateSession()
        Session("questionnaire_instrumentdetail_aspx") = gvQuestions.PageIndex
    End Sub

    Private Sub RetrieveStateSession()
        If String.IsNullOrEmpty(Session("questionnaire_instrumentdetail_aspx")) Then
            Exit Sub
        End If
        gvQuestions.PageIndex = CInt(Session("questionnaire_instrumentdetail_aspx"))
    End Sub


    Protected Sub InstrumentDS_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles InstrumentDS.Selected
        Try
            ViewState("IsNotLocked") = Not CType(e.ReturnValue, Instrument).Locked
        Catch ex As Exception
            Response.Redirect("InstrumentManager.aspx")
        End Try
        'Response.Write("InstrumentDS_Selected : " & (_instrument Is Nothing) & "<br/>")
    End Sub

End Class
